package site.xuqing.autohelperengine.local.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import site.xuqing.autohelper.bean.StepContainerBean;
import site.xuqing.autohelperengine.local.bean.StepLocalBean;
import site.xuqing.autohelperengine.local.bean.StepLocalSaveBean;
import site.xuqing.autohelperengine.local.bean.StepLocalAddType;

/**
 * @author XuQing
 */
public class StepSaveUtil {
    private static final String SP_NAME = "AutoHelper";
    private static SharedPreferences sp;
    private static List<StepLocalSaveBean> local_data=new ArrayList<>();
    private static List<StepContainerBean> Step_data=new ArrayList<>();
    public static void init(Context applicationContext){
        if(sp==null){
            sp=applicationContext.getSharedPreferences(SP_NAME+applicationContext.getPackageName(), Context.MODE_PRIVATE);
        }
    }
    public static boolean saveLocalSteps(List<StepLocalBean> stepBeans, String name){
        getLocalData();
        for (StepLocalSaveBean stepLocalSaveBean :local_data){
            if (name.equals(stepLocalSaveBean.getName())){
                return false;
            }
        }
        StepLocalSaveBean stepLocalSaveBean =new StepLocalSaveBean();
        stepLocalSaveBean.setName(name);
        stepLocalSaveBean.setStepLocalType(StepLocalAddType.CREATE);
        stepLocalSaveBean.setStepBeans(stepBeans);
        local_data.add(stepLocalSaveBean);
        setLocalData();
        return true;
    }
    public static boolean saveSteps(StepContainerBean stepContainerBean1){
        getStepData();
        for (StepContainerBean stepContainerBean :Step_data){
            if (stepContainerBean1.getName().equals(stepContainerBean.getName())){
                return false;
            }
        }
        Step_data.add(stepContainerBean1);
        setStepData();
        return true;
    }

    public static List<StepLocalBean> getLocalStep(String name){
        getLocalData();
        for (StepLocalSaveBean stepLocalSaveBean :local_data){
            if (name.equals(stepLocalSaveBean.getName())){
                return stepLocalSaveBean.getStepBeans();
            }
        }
        return null;
    }

    public static StepContainerBean getStep(String name){
        getStepData();
        for (StepContainerBean stepStepSaveBean :Step_data){
            if (name.equals(stepStepSaveBean.getName())){
                return stepStepSaveBean;
            }
        }
        return null;
    }

    public static List<StepLocalSaveBean> getLocalSteps(){
        getLocalData();
        return local_data;
    }

    public static List<StepContainerBean> getSteps(){
        getStepData();
        return Step_data;
    }

    public static boolean deleteLocalStep(String name){
        getLocalData();
        for (StepLocalSaveBean stepLocalSaveBean :local_data){
            if (name.equals(stepLocalSaveBean.getName())){
                if (local_data.remove(stepLocalSaveBean)){
                    setLocalData();
                    return true;
                }else{
                    return false;
                }
            }
        }
        return false;
    }

    public static boolean deleteStep(String name){
        getStepData();
        for (StepContainerBean stepStepSaveBean :Step_data){
            if (name.equals(stepStepSaveBean.getName())){
                if (Step_data.remove(stepStepSaveBean)){
                    setStepData();
                    return true;
                }else{
                    return false;
                }
            }
        }
        return false;
    }

    private static void getLocalData() {
        System.out.println(sp.getString("local_steps","[]"));
        local_data=new Gson().fromJson(sp.getString("local_steps","[]"), new TypeToken<List<StepLocalSaveBean>>(){}.getType());
    }

    private static void getStepData() {
        System.out.println(sp.getString("steps","[]"));
        Step_data=new Gson().fromJson(sp.getString("steps","[]"), new TypeToken<List<StepContainerBean>>(){}.getType());
    }

    private static void setLocalData() {
        System.out.println(new Gson().toJson(local_data,new TypeToken<List<StepLocalSaveBean>>(){}.getType()));
        sp.edit().putString("local_steps",new Gson().toJson(local_data,new TypeToken<List<StepLocalSaveBean>>(){}.getType())).apply();
    }

    private static void setStepData() {
        System.out.println(new Gson().toJson(Step_data,new TypeToken<List<StepContainerBean>>(){}.getType()));
        sp.edit().putString("steps",new Gson().toJson(Step_data,new TypeToken<List<StepContainerBean>>(){}.getType())).apply();
    }
}
